package com.tongji.diy.model.furniturePlacementOptimization.assembleComplement
{
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Vector3D;


	public class FacalChildBlock extends FurnitureBlock
	{
		private var _furVector:Vector.<FurnitureBase> = new Vector.<FurnitureBase>;
		
		private var _seatList:Vector.<FurnitureBase>  = new Vector.<FurnitureBase>;
		
		private var _beddingList:Vector.<FurnitureBase> = new Vector.<FurnitureBase>;
		
		
		private var initX:Boolean = true;
		private var initY:Boolean = true;
		
		public function FacalChildBlock(furVector:Vector.<FurnitureBase>,furnitrueIndex:int = -1, furnitureClass:int = -1, aganistWall:Boolean = true, length:Number=0, width:Number=0, height:Number=0)
		{
			this._furVector = furVector;

			var _location:Point = new Point();//存储坐标值
			var LWH:Vector3D = new Vector3D();//存储长宽高
			getVarities(_location,LWH);
			super(furnitrueIndex, furnitureClass, aganistWall, LWH.x, LWH.y, LWH.z);
			
			this.x = _location.x;
			this.y = _location.y;
		}
		
		public function mirrorRotate():void
		{
			for(var i:int = 0; i < _furVector.length; ++i)
			{
				_furVector[i].x = 2 * this.x - _furVector[i].x;
				_furVector[i].y = 2 * this.y - _furVector[i].y;
				_furVector[i].angle += Math.PI;
			}
		}
		
		private function getVarities(location:Point, LWH:Vector3D):void
		{
			var minX:Number = Infinity;
			var minY:Number = Infinity;
			var maxX:Number = -Infinity;
			var maxY:Number = -Infinity;
			
			
			for(var i:int = 0; i < _furVector.length; ++i)
			{
				var fur:FurnitureBase = _furVector[i];
				
				if(fur.height > height)
					LWH.z = fur.height;
				
				if(fur.minX < minX)
					minX = fur.minX;
				if(fur.minY < minY)
					minY = fur.minY;
				if(fur.maxX > maxX)
					maxX = fur.maxX;
				if(fur.maxY > maxY)
					maxY = fur.maxY;

				if(fur is FacalCompound)
					LWH.z = (fur as FacalCompound).container.height + (fur as FacalCompound).containee.height;
				
				if(fur is Bedding)
					_beddingList.push(fur);
				else if(fur is Seat)
					_seatList.push(fur);
			}
			
			
			LWH.y = maxX - minX;
			LWH.x = maxY - minY;
			location.x = (maxX + minX)/2;
			location.y = (maxY + minY)/2;		
		}
		
		override public function get furVector():Vector.<FurnitureBase>
		{
			return _furVector;
		}
		
		public function get seat():Vector.<FurnitureBase>
		{
			return _seatList;
		}
		
		public function get bedding():Vector.<FurnitureBase>
		{
			return _beddingList;
		}
		
		public override function set x(newX:Number):void
		{
			if(initX)
			{
				initX = false;
				super.x = newX;
				return;
			}
			var tempx:Number = x;
			super.x = newX;
			for(var i:int = 0; i < _furVector.length; ++i)
				_furVector[i].x += newX - tempx; 
			_accessibleBox = buildAccessOBB(x,y,width,length);
		}
		
		public override function set y(newY:Number):void
		{
			if(initY)
			{
				initY = false;
				super.y = newY;
				return;
			}
			var tempy:Number = y;
			super.y = newY;
			for(var i:int = 0; i < _furVector.length; ++i)
				_furVector[i].y += newY - tempy; 
			_accessibleBox = buildAccessOBB(x,y,width,length);
		}
		
		public override function set angle(newAngle:Number):void
		{
			var tempangle:Number = super.angle;
			super.angle = newAngle;
	
			var m1:Matrix = new Matrix;
			var m2:Matrix = new Matrix;
			var r:Matrix = new Matrix;
			r.rotate(-newAngle + tempangle);
			m1.identity();
			m2.identity();
			m1.translate(-this.x,-this.y);
			m2.translate(this.x,this.y);
			m1.concat(r);
			m1.concat(m2);
			for(var i:int = 0; i < _furVector.length; ++i)
			{	
				var fur:FurnitureBase = _furVector[i];
				var resultPoint:Point  = m1.transformPoint(new Point(fur.x,fur.y));
				
				fur.x = resultPoint.x;
				fur.y = resultPoint.y;
				fur.angle += newAngle - tempangle;
			}
			
			_accessibleBox = buildAccessOBB(x,y,width,length);

		}
	}
}